﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Portfolio : System.Web.UI.Page
{
    protected string sImages = string.Empty;
    protected int CurrentPage=0;
    protected int iPageSize = 4;
    private int iTotalPages;
    protected int WorkType = 5;
    protected LocalFunctions FUNC;
    protected string sTitle;
    protected DataTable DT;
    protected string sKeywords = string.Empty;
    protected string sDescription = string.Empty;

    protected void Page_Load(object sender, EventArgs e)
    {

        FUNC = ((ArtCube)this.Master).FUNC;

        sTitle = FUNC.GetPhrase(2);
        //Get page global data
        Dal.arParams.Clear();
        Dal.arParams.Add(5); // typeID - 1- Emoloyees
        DataTable DTPage = Dal.ExecuteDBsp("Admin_GetPageByID", Dal.arParams);

        // If not null - fill fields
        if (DTPage != null)
            if (DTPage.Rows.Count > 0)
            {
                DataRow Row = DTPage.Rows[0];

                this.Title = Row["PageTitle"].ToString();
                sKeywords = Row["Keywords"].ToString();
                sDescription = Row["Description"].ToString();

            }


        GetAttributes();
        //Load pageData
        Dal.arParams.Clear();
        Dal.arParams.Add(WorkType); // Work Type
        Dal.arParams.Add((int)((ArtCube)this.Master).FUNC.LangID); // Curent language
        Dal.arParams.Add(CurrentPage);
        Dal.arParams.Add(iPageSize);
        DT = Dal.ExecuteDBsp("GetWorksWPaging", Dal.arParams);

        // If not null - fill fields
        if (DT != null)
            // Set Article
            if (DT.Rows.Count > 0)
            {
                iTotalPages = (int)Math.Ceiling(Convert.ToDouble(DT.Rows[0]["TotalRows"]) / iPageSize);

                
                if (iTotalPages > 1)
                    SimplePager();
            }
    }

    private void GetAttributes()
    {
        CurrentPage=Convert.ToInt16(Request.QueryString["Page"]);
        CurrentPage = (CurrentPage == 0) ? 1 : CurrentPage;

        WorkType = Convert.ToInt16(Request.QueryString["wt"]);
        WorkType = (WorkType == 0) ? 5 : WorkType;
        //hightlight submenu
        switch(WorkType)
        {
            case 1:
                lnkIllustrator.CssClass = "btnIlustratorSelected";
                break;
            case 2:
                lnkBrand.CssClass = "btnBrandSelected";
                break;
            case 3:
                lnkAnimation.CssClass = "btnAnimationSelected";
                break;
            case 4:
                lnkPrint.CssClass = "btnPrintSelected";
                break;
            case 5:
                lnkWeb.CssClass = "btnWebSelected";
                break;
            default:
                break;
        }
    }

    public void SimplePager()
    {
        if (CurrentPage < 0) CurrentPage = 1;
        if (iTotalPages < 0) iTotalPages = 0;

        string sLink = string.Format("Portfolio.aspx?page=$Page$&wt={0}", WorkType);

        var pager = new PagerBuilder(sLink);

        if (CurrentPage > 1)
        {
            pager.AddPage("&gt;&gt;", 1);
            pager.AddPage("&gt;", CurrentPage - 1);
        }

        var start = Math.Max(CurrentPage - 2, 1);
        var end = Math.Min(iTotalPages, CurrentPage + 2);

        for (var i = start; i <= end; i++)
        {
            if (i == CurrentPage)
                pager.AddPage(i.ToString(), i, "CurrentPage");
            else
                pager.AddPage(i.ToString(), i);
        }

        if (CurrentPage < iTotalPages)
        {
            pager.AddPage("&lt;&lt;", CurrentPage + 1);
            pager.AddPage("&lt;", iTotalPages);
        }

        ulPager.InnerHtml +=string.Format("<ul  class='ulPager'>{0}</ul>",pager.ToString());
    }
}


